package com.concerneyes;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.concerneyes.mine.TabActivity.MyExtension;
import com.concerneyes.mine.TabActivity.serviceCentre.CheckLog;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.widget.ImageView;

import common.BaseActivity;
import common.PreferUtil;
import util.ImageUtil;
import util.Util;


/**
 * Depiction: 闪屏界面
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>
 * Create Date：2016年1月21日 下午12:50:29
 * <p>
 *
 * @version 1.0
 * @since 1.0
 */
public class SplashActivity extends Activity {

    private ImageView splashView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Util.isFirst(this)) {
            //首次安装或者升级为新版本,开启引导页面
            startGuide();
        } else {
            startSplash();
        }
    }

    private void startGuide() {
        Intent intent = new Intent(this, GuideActivity.class);
        intent.putExtra("from_splash", true);
        startActivity(intent);
        finish();
    }

    private void startSplash() {
        setContentView(R.layout.activity_splash);
        splashView = (ImageView) findViewById(R.id.imageIv);
        Glide.with(getApplicationContext()).load(R.drawable.splash).listener(new RequestListener<Integer, GlideDrawable>() {

            @Override
            public boolean onException(Exception e, Integer model, Target<GlideDrawable> target, boolean isFirstResource) {
                enterMain();
                return false;
            }

            @Override
            public boolean onResourceReady(GlideDrawable resource, Integer model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                splashView.setImageDrawable(resource);
                enterMain();
                return false;
            }
        }).placeholder(R.drawable.splash).into(splashView);
    }

    private void enterMain() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = null;
                if (!TextUtils.isEmpty(PreferUtil.getUserId())) {
                    intent = new Intent(SplashActivity.this, MainActivity.class);
                } else {
                    intent = new Intent(SplashActivity.this, LoginActivity.class);
                }
                startActivity(intent);
                finish();
            }
        }, 300);
    }
}
